﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.ComponentModel;

namespace Accountability4net.Contact
{
    public class BaseContact: IContact
    {
        public BaseContact()
        {
        }

        /// <summary>
        /// Notify listeners that a property has changed
        /// </summary>
        /// <param name="propertyName">The name of the property that has changed</param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChangedEventArgs e = new PropertyChangedEventArgs(propertyName);
                PropertyChanged(this, e);
            }
        }

        protected virtual void OnAddressChanged(ListChangedEventArgs e)
        {
            if (AddressChanged != null)
            {
                AddressChanged(Address, e);
            }
        }

        protected virtual void OnTelephoneChanged(ListChangedEventArgs e)
        {
            if (TelephoneChanged != null)
            {
                TelephoneChanged(Telephone, e);
            }
        }

        /// <summary>
        /// Notification for property changes
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        public event ListChangedEventHandler AddressChanged;
        public event ListChangedEventHandler TelephoneChanged;

        #region IContact Members

        protected IName name_;
        public virtual IName Name
        {
            get
            {
                if (name_ == null)
                {
                    name_ = new BaseName();
                }
                return name_;
            }
            set
            {
                name_ = value;
                OnPropertyChanged("Name");
            }
        }

        private Image photo_;
        public Image Photo
        {
            get
            {
                return photo_;
            }
            set
            {
                photo_ = value;
                OnPropertyChanged("Photo");
            }
        }

        private DateTime birthDate_;
        public DateTime Birthdate
        {
            get
            {
                return birthDate_;
            }
            set
            {
                birthDate_ = value;
                OnPropertyChanged("Birthdate");
            }
        }

        private EventedList<IAddress> address_;
        public EventedList<IAddress> Address
        {
            get
            {
                if (address_ == null)
                {
                    address_ = new EventedList<IAddress>();
                    address_.ListChanged += new ListChangedEventHandler(address__ListChanged);
                }
                return address_;
            }
        }

        void address__ListChanged(object sender, ListChangedEventArgs e)
        {
            OnAddressChanged(e);
        }

        void telephone__ListChanged(object sender, ListChangedEventArgs e)
        {
            OnTelephoneChanged(e);
        }

        private EventedList<ITelephoneNumber> telephone_;
        public EventedList<ITelephoneNumber> Telephone
        {
            get
            {
                if (telephone_ == null)
                {
                    telephone_ = new EventedList<ITelephoneNumber>();
                    telephone_.ListChanged += new ListChangedEventHandler(telephone__ListChanged);
                }
                return telephone_;
            }
        }

        private string email_;
        public string Email
        {
            get
            {
                return email_;
            }
            set
            {
                email_ = value;
                OnPropertyChanged("Email");
            }
        }

        private TimeZone timeZone_;
        public TimeZone TimeZone
        {
            get
            {
                return timeZone_;
            }
            set
            {
                timeZone_ = value;
                OnPropertyChanged("TimeZone");
            }
        }

        private string title_;
        public string Title
        {
            get
            {
                return title_;
            }
            set
            {
                title_ = value;
                OnPropertyChanged("Title");
            }
        }

        private string role_;
        public string Role
        {
            get
            {
                return role_;
            }
            set
            {
                role_ = value;
                OnPropertyChanged("Role");
            }
        }

        private string organization_;
        public string Organization
        {
            get
            {
                return organization_;
            }
            set
            {
                organization_ = value;
                OnPropertyChanged("Organization");
            }
        }

        private string organizationalUnit_;
        public string OrganizationalUnit
        {
            get
            {
                return organizationalUnit_;
            }
            set
            {
                organizationalUnit_ = value;
                OnPropertyChanged("OrganizationalUnit");
            }
        }

        private string comment_;
        public string Comment
        {
            get
            {
                return comment_;
            }
            set
            {
                comment_ = value;
                OnPropertyChanged("Comment");
            }
        }

        private Uri web_;
        public Uri Web
        {
            get
            {
                return web_;
            }
            set
            {
                web_ = value;
                OnPropertyChanged("Web");
            }
        }

        private Guid id_;
        public Guid ID
        {
            get
            {
                return id_;
            }
            set
            {
                id_ = value;
                OnPropertyChanged("ID");
            }
        }

        #endregion
    }
}
